from aiapi.utils.users import get_login_user_id
from django.core.cache import cache
from aiapi.models.users import Users
from aiapi.utils.data_handle import users_data_paging


def get_login_user(request):
    # 先查询 user_id
    userid = get_login_user_id(request, isadmin=False)

    if not userid:
        return int(0)
    
    # 先从redis缓存中获取用户信息
    ## 暂时关闭
    user_cache_mes = cache.get(f'user_info_{userid}')
    if user_cache_mes:
        return user_cache_mes
    else:
        # 获取用户信息
        try:
            re_user_obj = Users.objects.filter(id=userid).order_by('id')

            # 设置用户信息缓存到redis
            chatusers_tree_json = users_data_paging(re_user_obj, 1, 10)
            cache.set(f'user_info_{userid}', chatusers_tree_json["items"][0], timeout=3600)  # 设置缓存超时时间为3600秒

            return chatusers_tree_json['items'][0]
        except Users.DoesNotExist:
            return int(0)

